package edy.ynmd.cms.service.impl;

import edy.ynmd.cms.dao.*;
import edy.ynmd.cms.model.*;
import edy.ynmd.cms.service.ManageService;
import edy.ynmd.cms.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private NewsDao newsDao;

    @Autowired
    private CarouselDao carouselDao;

    @Autowired
    private UsersDao usersDao;

    @Autowired
    private BookDao bookDao;

    @Autowired
    private CourseDao courseDao;


    @Override
    public boolean addNews(News news) {

        try {
            newsDao.save(news);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public boolean deletenews(String id) {
        try {
            newsDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public boolean updateNews(News news) {
        try {
            newsDao.save(news);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public News getNews(String id) {
        return newsDao.findOne(id);
    }

    @Override
    public List<News> getNewsList() {
        return newsDao.findAll();
    }

    @Override
    public boolean addCarousel(Carousel carousel) {
        try {
            carouselDao.save(carousel);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean delteCarousel(String id) {
        try {
            carouselDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public boolean updateCarousel(Carousel carousel) {

        try {
            carouselDao.save(carousel);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public Carousel getCarousel(String id) {
        return carouselDao.findOne(id);
    }

    @Override
    public List<Carousel> getCarouselList() {
        return carouselDao.findAll();
    }


    @Override
    public Users findUserByUsernameAndPassword(String username, String password) {
        return usersDao.getUsersByUsernameAndPassword(username, password);
    }

    @Override
    public String getCurrentUserId() {
        String userid = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (Tools.isNullOrSpace(userid)) {
            return null;
        } else {
            return userid;
        }

    }


        @Override
        public String getCurrentUserRole () {
            String role = null;
            Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>) SecurityContextHolder.getContext().getAuthentication().getAuthorities();
            for (GrantedAuthority authority : authorities) {
                role = authority.getAuthority();

            }

            if (Tools.isNullOrSpace(role)) {
                return null;
            } else {
                return role;
            }

        }


    @Override
    public boolean addBook(Book book) {

        try {
            bookDao.save(book);
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
        }



        return false;
    }

    @Override
    public boolean deleteBook(String id) {

        try {
            bookDao.delete(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public boolean updateBook(Book book) {
        try {
            bookDao.save(book);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Book getBook(String id) {
        return bookDao.findOne(id);
    }

    @Override
    public List<Book> getBookList() {
        return bookDao.findAll();
    }


    @Override
    public boolean addCourse(Course course) {
        try {
            courseDao.save(course);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public List<Course> getCourseByBook(String bookid) {
        return courseDao.getCourseListByBook(bookid);
    }
}

